import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { NzFormatEmitEvent, NzTreeNodeOptions } from 'ng-zorro-antd';
import { DataDictionaryService } from 'services';

@Component({
    moduleId: module.id,
    selector: 'data-tree',
    templateUrl: 'data-tree.component.html',
    providers: [DataDictionaryService]
})
export class DataTreeComponent implements OnInit {
    @Input() groupCode: string;
    _selectCode: string;
    @Output() selectCodeChange = new EventEmitter();

    @Input()
    get selectCode() {
        return this._selectCode;
    }
    set selectCode(val) {
        this._selectCode = val;
        this.selectCodeChange.emit(this._selectCode);
    }

    @Output() selectTreechange = new EventEmitter<NzFormatEmitEvent>();
    dataTrees: NzTreeNodeOptions[] = [];
    selectedKeys = [];
    constructor(private dictionaryService: DataDictionaryService) {
    }
    ngOnInit(): void {
        this.getTreeNode();
    }

    getTreeNode() {
        this.dictionaryService.getDataGroupNzTree(this.groupCode, this.selectCode).subscribe(result => {
            this.dataTrees = result;
            if (!this.selectCode && this.dataTrees.length > 0) {
                this.selectCode = this.dataTrees[0].key;
                this.click({ node: this.dataTrees[0] });
            }
            this.selectedKeys = [this.selectCode];
        });
    }

    click(event: any) {
        this.selectTreechange.emit(event);
    }
}
